package net.e_agri.json;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.arnx.jsonic.JSON;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AlbumConverter extends AbstractContentConverter implements ContentConverter {

	private static final Log log = LogFactory.getLog(AlbumConverter.class);

	@Override
	public String parseSource(Source source, String url) {

		List<List<Object>> contentList = new ArrayList<List<Object>>();
		@SuppressWarnings("rawtypes")
		Map<String, List> contentMap = new LinkedHashMap<String, List>();
		List<Object> record = new ArrayList<Object>();
		List<String> imgs = new ArrayList<String>();

		contentMap.put("fields", Arrays.asList("title","url","auther","date","body","images"));
		Map<String, String> coverMap = new LinkedHashMap<String, String>();
		coverMap.put("タイトル", "");
		coverMap.put("作成日時", "");
		coverMap.put("説明文", "");

		String thVal = "";
		String tdVal = "";
		String author = "";

		for(Element div : source.getAllElements(HTMLElementName.DIV)){
			if("dparts albumDetailBox".equals(div.getAttributeValue("class"))){
				for(Element d : div.getAllElements(HTMLElementName.DIV)){
					if("partsHeading".equals(d.getAttributeValue("class"))){
						author = d.getTextExtractor().toString().replace("のアルバム","");
						break;
					}
				}
				break;
			}
		}

		boolean flgTH = false;
		//Album header
		for(String key : coverMap.keySet()){
			for(Element tr : source.getFirstElement(HTMLElementName.TABLE).getAllElements(HTMLElementName.TR)){
				for(Element t : tr.getChildElements()){

					if(t.getName().equals(HTMLElementName.TH)){
						flgTH = true;
						thVal = t.getTextExtractor().toString();
					}else if(flgTH){
						flgTH = false;
						if(t.getName().equals(HTMLElementName.TD)){
							tdVal = t.getTextExtractor().toString();

							if(thVal.equals(key)){
								log.debug("hit:" + key + ", " + tdVal);
								
								if("作成日時".equals(thVal)){
									record.add(tdVal
											.replace("年", "/")
											.replace("月", "/")
											.replace("日", ""));
								}else{
									record.add(tdVal);

									if("タイトル".equals(thVal)){
										record.add("N/A");
										record.add(author);
									}
								}
			
								break;
							}
						}
					}
				}
			}
		}

		String cacheUrl = null;
		String imageUrl = null;

		for(Element tr : source.getFirstElement(HTMLElementName.TABLE).getAllElements(HTMLElementName.TR)){
			if("photo".equals(tr.getFirstElement(HTMLElementName.TD).getAttributeValue("class"))){
				
				imageUrl = uriRoot + tr.getFirstElement(HTMLElementName.TD)
				.getFirstElement(HTMLElementName.IMG).getAttributeValue("src").replace("&m=pc", "");

				cacheUrl = getImageCacheUrl(imageUrl);
				if(cacheUrl != null)
					imgs.add(cacheUrl);
				else
					imgs.add(imageUrl);

				break;
			}
		}
		record.add(imgs);
		contentList.add(record);

		//Album photos
		Element fp = null;
		for(Element td : source.getAllElements(HTMLElementName.TD)){
			for(Element p : td.getAllElements(HTMLElementName.P)){
				if("text".equals(p.getAttributeValue("class"))){
					record = new ArrayList<Object>();

					//"title","url", "auther","date","body","images"
					record.add(p.getContent().toString());

					imgs = new ArrayList<String>();
					fp = td.getFirstElement(HTMLElementName.P);
					if("image".equals(fp.getAttributeValue("class"))){
						
						imageUrl = uriRoot
						+ fp.getFirstElement(HTMLElementName.IMG).getAttributeValue("src").replace("&m=pc", "");

						cacheUrl = getImageCacheUrl(imageUrl);
						if(cacheUrl != null)
							imgs.add(cacheUrl);
						else
							imgs.add(imageUrl);

						for(Element a : fp.getAllElements(HTMLElementName.A))
							if(a.getAttributeValue("href").contains("edit")){
								record.add(a.getAttributeValue("href"));
								break;
							}
					}

					if(record.size() == 1)
						record.add("N/A");

					record.add("");
					record.add("");
					record.add("");
					record.add(imgs);
					
					if(record.size() != contentMap.keySet().size())
						log.warn("invalid record size : " + record.toString());
						
					contentList.add(record);

					break;
				}
			}
		}

		contentMap.put("data", contentList);

		return JSON.encode(contentMap);
	}

}
